﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataLib.Accounts.Bus;

namespace Ewts.Storage
{

    /// <summary>
    /// 生产入库
    /// </summary>
    public partial class ProduceStorage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindMonth();
                if (Session["UserInfo"] != null)
                {
                    User curUser = new DataLib.Accounts.Bus.User();
                    curUser = (User)Session["UserInfo"];
                    if (curUser.UserType == "AD")
                        this.hidgraphic.Value = "";
                    else
                    {
                        this.hidgraphic.Value = curUser.CompanyCity;
                    }
                    SetDisplay();
                    HiddenOper.Value = curUser.UserName;
                }



                dataBind();

            }
        }

        protected void SetDisplay()
        {

            ///从出库单过来
            if (Request["outid"] != null && Request["outid"].ToString().Trim() != "")
            {
                BindOutid(Request["outid"].ToString(), null);
            }
            else if (Request["insid"] != null && Request["insid"].ToString().Trim() != "")
            {
                DataLib.Model.InStorage insModel = new DataLib.BLL.InStorage().GetModel(int.Parse(Request["insid"].ToString().Trim()));
                if (insModel.clearingmonth != null && insModel.clearingmonth.Length == 6)
                {
                    string stryear = insModel.clearingmonth.Substring(0, 4);
                    string strmonth = insModel.clearingmonth.Substring(4, 2);
                    dropYear.SelectedValue = stryear;
                    dropMonth.SelectedValue = strmonth;
                }
                BindOutid(insModel.outid.ToString(), insModel);
            }


        }

        /// <summary>
        /// 第一次入库创建入库编号
        /// </summary>
        /// <param name="outModel"></param>
        /// <returns></returns>
        protected string GetSerialNum(string outtype)
        {
            string strNum = "";
            string strInLeft = "RS";
            HidStatus.Value = "5";

            hidInstype.Value = strInLeft;
            //strNum = Code.NumGeneration.GetSerialNum(strInLeft, "InCode");

            return strNum;
        }

        /// <summary>
        /// 获取BarCode的是否锁定状态
        /// </summary>
        /// <param name="islocak"></param>
        /// <returns></returns>
        protected string GetIsLock(string islocak)
        {
            if (Boolean.Parse(islocak))
                return "checked";
            else
                return "";
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        /// <param name="loadID"></param>
        protected void dataBind()
        {
            //根据出库ID取得入库ID
            string outid = hidoutparID.Value;
            DataLib.BLL.BarCode bllBarCode = new DataLib.BLL.BarCode();
            int instroingid = int.Parse(hidInStoID.Value);

            if (instroingid != 0)
            {

                //处理前 分类汇总的数据绑定
                DataTable dtun = new DataTable();
                dtun = new DataLib.BLL.unProcessed().GetList("inSid=" + instroingid.ToString()).Tables[0];
                reptun.DataSource = dtun;
                reptun.DataBind();

                //入库数据绑定
                DataTable dtbar = new DataTable();
                dtbar = new DataLib.BLL.BarCode().GetList("InStrorageID=" + instroingid.ToString().Trim()).Tables[0];
                reptin.DataSource = dtbar;
                reptin.DataBind();

                //辅料绑定
                DataTable dtfl = new DataTable();
                dtfl = new DataLib.BLL.ins_fuliao().GetList("instroageid=" + instroingid.ToString().Trim()).Tables[0];
                reptFl.DataSource = dtfl;
                reptFl.DataBind();

                //损耗绑定
                DataTable dtsh = new DataTable();
                dtsh = new DataLib.BLL.ins_sunhao().GetList("instroageid=" + instroingid.ToString().Trim()).Tables[0];
                reptSh.DataSource = dtsh;
                reptSh.DataBind();
            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (hidInStoID.Value.Trim() == "" || hidInStoID.Value.Trim() == "0")
            {
                DataLib.Common.MessageBox.Show(this.Page, "不要提交空数据");
                return;
            }
            string insid = hidInStoID.Value;
            DataLib.BLL.InStorage bllins = new DataLib.BLL.InStorage();

            DataLib.Model.InStorage insModel = bllins.GetModel(int.Parse(insid));

            if (bllins.GetConfirm(0, (int)insModel.id, 3) == false)
                DataLib.Common.MessageBox.Show(Page,"超出重量");
            int outid = (int)insModel.outid;

            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            DataLib.BLL.OutStorage bllOut = new DataLib.BLL.OutStorage();
            outModel = bllOut.GetModel(outid);
            string outtype = "0";

            DataLib.BLL.InStorage bllIn = new DataLib.BLL.InStorage();
            if (outModel != null)
            {
                outtype = outModel.outStorageType;
            }
            else
            {
                outtype = "7";
            }
            string month = dropYear.SelectedValue + dropMonth.SelectedValue;
            bllIn.InstorageSubmit(int.Parse(insid), outtype,month);


            DataLib.Common.MessageBox.ShowAndRedirect(Page, "提交成功，请选择打印", "/Storage/SelectReport.aspx?insid=" + hidInStoID.Value);

        }




        protected void btnCanel_Click(object sender, EventArgs e)
        {
            DataLib.Common.MessageBox.ShowAndRedirect(Page, "跳转测试", "/Storage/SelectReport.aspx?insid=" + hidInStoID.Value);

        }

        private void BindOutid(string outid, DataLib.Model.InStorage imodel)
        {
            DataLib.Model.InStorage inModel = new DataLib.Model.InStorage();
            DataLib.Model.OutStorage outModel = new DataLib.Model.OutStorage();
            DataLib.Model.LoadNum loadModel = new DataLib.Model.LoadNum();

            DataLib.BLL.InStorage inBll = new DataLib.BLL.InStorage();
            DataLib.BLL.BarCode barBll = new DataLib.BLL.BarCode();
            DataLib.BLL.OutStorage outBll = new DataLib.BLL.OutStorage();
            DataLib.BLL.LoadNum loadBll = new DataLib.BLL.LoadNum();
            DataLib.BLL.ins_fuliao flBll = new DataLib.BLL.ins_fuliao();
            DataLib.BLL.ins_sunhao shBll = new DataLib.BLL.ins_sunhao();


            //  if(imodel != null)

            hidoutparID.Value = outid;
            outModel = outBll.GetModel(int.Parse(outid));
            DataTable inDt = inBll.GetList("outid=" + outid).Tables[0];
            //需要从出库单带出来信息
            inputLoadNum.Value = outModel.VLoadNum;
            HidVLoad.Value = outModel.VLoadNum;
            hidoutparID.Value = outModel.id.ToString();
            txtSrcOutSid.Value = outModel.outStorageNo;
            hidInDepart.Value = outModel.belogarea;
            hidOutDepart.Value = outModel.graphicCity;
            hidintype.Value = "S";

            if (outModel.VLoadNum.IndexOf("86") == 0)
            {
                //分类入库的时候
                loadModel = loadBll.GetLoadID(outModel.VLoadNum);
                DataLib.Model.Cumtomer cummodel = new DataLib.BLL.Cumtomer().GetModel((int)loadModel.customerid);
                txtcutomer.Value = cummodel.companyname;
                hidLoadID.Value = loadModel.id.ToString();

            }
            else
            {
                DataLib.Model.Vload vmodel = new DataLib.BLL.Vload().GetModelByOutID((int)outModel.id);
                if (vmodel != null)
                {
                    txtcutomer.Value = vmodel.inscumname;
                }
            }
            if (inDt != null && inDt.Rows.Count > 0)
            {
                //已有入库单的情况
                inModel = inBll.GetModel(int.Parse(inDt.Rows[0]["id"].ToString()));
                hidCJOID.Value = inModel.cjoid.ToString();

                txtin.Value = inModel.wrlno;
                hidInStoID.Value = inModel.id.ToString();

            }
            else
            {
                GetSerialNum(outModel.outStorageType);
            }

            spanTotal.InnerHtml = outModel.totalWeight.ToString();
            spanCurNwt.InnerHtml = outBll.GetInNwtByOutid(outModel.id.ToString()).ToString();
        }

        protected void bindMonth()
        {
            int year = DateTime.Now.Year;

            ListItem listitem = new ListItem((year + 1).ToString() + "年", (year + 1).ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem(year.ToString() + "年", year.ToString());
            dropYear.Items.Add(listitem);
            listitem = new ListItem((year - 1).ToString() + "年", (year - 1).ToString());
            dropYear.Items.Add(listitem);

            dropYear.SelectedValue = year.ToString();
            dropMonth.SelectedValue = DateTime.Now.Month.ToString().PadLeft(2, '0');


        }


    }
}